<?php

namespace App\Models;

use Carbon\Carbon;
use App\Models\Traits\GoldCoinsTrait;
use App\Models\Traits\HasScopeInquire;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Group extends Model
{
    use HasFactory, HasScopeInquire;

    protected $guarded = ['id'];

	protected $casts = [
		'data' => 'json'
	];

	public function scopeNoFinished(Builder $builder)
    {
        return $builder->where('is_finished', false)->where('group_expired_at','>',Carbon::now());
    }

    public function isFinished(): bool
    {
        return (bool)$this->is_finished;
    }

  

    public function user(): BelongsTo
    {
    	return $this->belongsTo(User::class);
    }

     public function product(): BelongsTo
    {
        return $this->belongsTo(Product::class);
    }

     public function groupCard(): BelongsTo
    {
        return $this->belongsTo(GroupCard::class);
    }

    public function records()
    {
        return $this->hasMany(GroupRecord::class);
    }

    protected function serializeDate(DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }
}
